package com.thinkgem.jeesite.modules.cms.web.login;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.CommonParameter;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.accountinfo.entity.AccountInfo;
import com.thinkgem.jeesite.modules.accountinfo.entity.AccountInfoAddress;
import com.thinkgem.jeesite.modules.accountinfo.service.AccountInfoAddressService;
import com.thinkgem.jeesite.modules.accountinfo.service.AccountInfoService;
import com.thinkgem.jeesite.modules.shop.entity.order.ShopOrder;
import com.thinkgem.jeesite.modules.shop.service.order.ShopOrderService;

@Controller
@RequestMapping(value = "${frontPath}/userfront")
public class AccountInfoFrontController extends BaseController{
    @Autowired
    private AccountInfoAddressService addressService;
    
    @Autowired
    private AccountInfoService accountInfoService;
    
    @Autowired
    private ShopOrderService shopOrderService;

    @RequestMapping("myAccount")
    public String toAddress(AccountInfoAddress address,HttpSession session,Model model){
        AccountInfo user = (AccountInfo) session.getAttribute("user");
        if(user.getQqNum()!=null&&user.getQqNum()!=""){
            String qqNickname = (String) session.getAttribute("qqNickname");
            model.addAttribute("qqNickname",qqNickname);
        }
        if(user.getWechatNum()!=null&&user.getWechatNum()!=""){
            String wxNickname = (String) session.getAttribute("weixinNickname");
            model.addAttribute("wxNickname",wxNickname);
        }
        model.addAttribute("accountInfo",accountInfoService.get(user.getId()));
        return "modules/account/myAccount"; 
    }
    
    @RequestMapping("toOrder")
    public String toOrder( ShopOrder shopOrder, @RequestParam(required = false, defaultValue = "1")Integer pageNo,@RequestParam(required = false, defaultValue = "5")Integer pageSize,HttpSession session,Model model){
    	 
        AccountInfo user = (AccountInfo) session.getAttribute("user");
        shopOrder.setAccountId(user.getId());
        Page<ShopOrder> page = shopOrderService.findByAccountId(new Page<ShopOrder>(pageNo, pageSize),shopOrder);
        model.addAttribute("page", page);
        model.addAttribute("path", CommonParameter.uploadPath);
        return "modules/account/myBooking"; 
    }
    
    @RequestMapping("toAddress2")
    public String toAddress2(AccountInfoAddress address,HttpSession session,Model model){
    	 
        AccountInfo user = (AccountInfo) session.getAttribute("user");
        List<AccountInfoAddress> list = addressService.addressList(user);
        model.addAttribute("addressList", list);
        if(StringUtils.isNotBlank(address.getId())){
            model.addAttribute("address",addressService.get(address));
        }
        return "modules/account/myDelivered"; 
    }
    
    @RequestMapping(value="saveAddress")
    public String address(AccountInfoAddress address,String id,String defAdd,String defaultAdd){
        AccountInfo user = accountInfoService.get(address.getAccountInfo().getId());
  
        List<AccountInfoAddress> list= addressService.findList(address); 

        if(list.size()>=10){
            return  "redirect:" + frontPath + "/userfront/toAddress2";
        }
        if(defAdd.equals("false")){
            address.setDefaultAddress(1);
            List<AccountInfoAddress> deflist= addressService.findListByAccountId(user); 
            if(!deflist.isEmpty()){
                for(AccountInfoAddress add:deflist){
                    if(address.getId()!=add.getId()){
                       add.setAccountInfo(user);
                       add.setDefaultAddress(0);
                       addressService.save(add);
                    }
                }
            }
        }else{
            address.setDefaultAddress(0);
        }
        if(list.size()==0){
            address.setDefaultAddress(1);
        }

        if(StringUtils.isNotBlank(address.getAddUsername())&&StringUtils.isNotBlank(address.getAddTelnum())
                &&StringUtils.isNotBlank(address.getProvince())&&StringUtils.isNotBlank(address.getCity())&&StringUtils.isNotBlank(address.getDetail())){
            address.setAccountInfo(user);
        
            addressService.save(address);
        }
        return  "redirect:" + frontPath + "/userfront/toAddress2";
    }
    
    @ResponseBody
    @RequestMapping("getAddress")
    public String getAddress(String id){
       
        AccountInfoAddress address = addressService.get(id);
     
        return JsonMapper.getInstance().toJson(address);
    }
    
    @RequestMapping("editDeafultAddress")
    public String editAddress(String id){
        if(id!=null){
            AccountInfoAddress address = addressService.get(id);
            List<AccountInfoAddress> list= addressService.findListByAccountId(accountInfoService.get(address.getAccountInfo().getId())); 
            if(!list.isEmpty()){
                for(AccountInfoAddress address1:list){
                    address1.setDefaultAddress(0);
                    address1.setAccountInfo(address.getAccountInfo());
                    addressService.save(address1);
                }
            }
            address.setDefaultAddress(1);
            addressService.save(address);
        }
        return  "redirect:" + frontPath + "/userfront/toAddress2";
    }
    
    @RequestMapping("delAddress")
    public String delAddress(String id){
        addressService.delete(addressService.get(id));
        return  "redirect:" + frontPath + "/userfront/toAddress2";
    }
}
